package net.caixiaomi.info.ui.discovery;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.LeagueDetailShooterInfo;
import net.caixiaomi.info.model.LeagueDetailShooterListEntity;
import net.caixiaomi.info.model.LeagueDetailTeamInfo;
import net.caixiaomi.info.model.LeagueDetailTeamListEntity;
import net.caixiaomi.info.model.LeagueInfoEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by mac on 2018/9/11.
 */

public class LeagueDetailTeamFragment extends BaseFragment implements ComonFragmentImpl,BaseQuickAdapter.OnItemClickListener{

    private LeagueDetailTeamListEntity entity;

    @BindView(android.R.id.list)
    RecyclerView mListView;

    @BindView(R.id.no_data_tv)
    TextView mNoData;

    private LeagueTeamAdapter mAdapter;

    @Override
    public void setType(int type) {

    }

    @Override
    public void setFilter(String filter) {

    }

    @Override
    protected int fragmentLayout() {
        return R.layout.league_shooter_layout;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        mListView.setLayoutManager(new GridLayoutManager(getActivity(),4));
        mAdapter = new LeagueTeamAdapter(R.layout.discovery_main_select_item);
        mAdapter.setOnItemClickListener(this);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent i=new Intent(getActivity(),TeamDetailActivity.class);
        LeagueDetailTeamInfo info= (LeagueDetailTeamInfo) adapter.getData().get(position);
        i.putExtra("teamId",info.getTeamId());
        startActivity(i);
    }

    public class LeagueTeamAdapter extends BaseQuickAdapter<MultiItemEntity, BaseViewHolder> {

        public LeagueTeamAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, MultiItemEntity item) {
            try {
                LeagueDetailTeamInfo hall = (LeagueDetailTeamInfo) item;
                String name = hall.getTeamAddr();
                String Imgurl = hall.getTeamPic();
                helper.setText(R.id.title, name);
                ImageView imageView = helper.getView(R.id.image);
                Glide.with(imageView.getContext()).load(Imgurl).into(imageView);
            }catch (Exception e){

            }
        }
    }

    public void initData(LeagueDetailTeamListEntity entity){
        this.entity=entity;
        List<LeagueDetailTeamInfo> leagueShooterInfoList=entity.getLeagueTeamInfoDTOList();
        if(leagueShooterInfoList.size()==0){
            mNoData.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
            return;
        }
        List<MultiItemEntity>list=new ArrayList<>();
        list.addAll(leagueShooterInfoList);
        mAdapter.setNewData(list);
    }
}
